package
{
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	
	public class MomoTank
	{
		public var name:String = "tank";
		// 模型
		private var g_pTankBody:TankBody = null;
		private var g_pTankGun:TankGun = null;
		//
		public function get shape():Sprite{
			return m_pBody;
		}
		//外形
		private var m_pBody:Sprite = new Sprite;
		private var m_pGun:Sprite = new Sprite;
		//具体数据
		private var m_iGunAngle:Number = 0;
		private var m_iDefPoint:int = 100;
		private var m_iHoldPoint:int = 100;
		//来自mode的数据
		public function get gun():TankGun{
			return g_pTankGun;
		}
		public function get body():TankBody{
			return g_pTankBody;
		}
		//坐标
		public var z:Number = 0;//高度,保留
		public function get x():Number{
			return m_pBody.x;
		}
		public function set x( v:Number ):void{
			m_pBody.x=v;
		}
		public function get y():Number{
			return m_pBody.y;
		}
		public function set y( v:Number ):void{
			m_pBody.y=v;
		}
		public function get rotation():Number{
			return m_pBody.rotation;
		}
		public function set rotation( v:Number ):void{
			m_pBody.rotation=v;
		}
		//炮口坐标
		public function get gunX():Number{
			return x+body.gunSetX+gun.startX;
		}
		public function get gunY():Number{
			return y+body.gunSetY;
		}
		public function get gunRotation():Number{
			return m_pGun.rotation-rotation;
		}
		//所属
		private var m_pPlayer:Player = null;//null-电脑 其他-玩家?
		private var m_iForce:int = 0;//队伍编号 0无队伍 1以上-临时或编制军团
		private var m_iClass:int = 0;//小队编号 0无小队 1以上-小队编号
		//控制移动和炮塔转向参数
		private var m_iMx:Number = 0;//拖放操作起点
		private var m_iMy:Number = 0;
		private var m_iDragStyle:int = 0x00; //0x00-车身起, 0x10-地面起
		private var m_bMovePointer:Boolean = false;//移动指令
		private var m_bFrontWard:Boolean = true;//向前
		private var m_iDirect:int = 0;//向左 -1 向右 +1
		private var m_iRotation:Number = 0;//目标朝向
		private var m_iPx:Number = 0;//目标坐标
		private var m_iPy:Number = 0;
		private var m_iSpeed:Number = 2;//实际车速,限定最大车速1
		private var m_iRad:Number = 1;//实际角速度,限定最大角速度1
		private var m_bShotPointer:Boolean = false;//射击指令
		private var m_bQuickShot:Boolean = true;//立即射击 - 符合目标射向即射击 - 拖放在最小射程内设为false,否则为true
		private var m_iGunRotation:Number = 0;//目标射向 - 真方位
		//访问方法
		public function get hp():int{
			return m_iHoldPoint;
		}
		public function get dp():int{
			return m_iDefPoint;
		}
		public function get maxHP():int{
			return g_pTankBody.maxHP();
		}
		public function get maxDP():int{
			return g_pTankBody.maxDP();
		}
		//
		public function MomoTank( type:int, x:Number, y:Number, r:Number, p:Player )
		{
			InitTexture();
			this.x=x;
			this.y=y;
			this.rotation=r;
			this.m_pPlayer=p;
		}
		//
		public function checkObject( o:DisplayObject ):Boolean{
			return m_pBody.hitTestObject(o);
		}
		//
		public function InitTexture():void{
			//添加炮
			var gun:Shape = new Shape;
			var g:Graphics = gun.graphics;
			g.lineStyle(1,0,0);
			g.beginBitmapFill(g_pTankGun.bmpData,null,true,true);
			g.drawRect(0,0,g_pTankGun.bmpData.width,g_pTankGun.bmpData.height);
			g.endFill();
			gun.x-=g_pTankBody.gunSetX;
			gun.y-=g_pTankBody.gunSetY;
			m_pGun.addChild(gun);
			//添加车体
			var body:Shape = new Shape;
			g = body.graphics;
			g.lineStyle(1,0,0);
			g.beginBitmapFill(g_pTankBody.bmpData,null,true,true);
			g.drawRect(0,0,g_pTankBody.bmpData.width,g_pTankBody.bmpData.height);
			g.endFill();
			body.x-=g_pTankBody.width;
			body.y-=g_pTankBody.height;
			m_pBody.addChild(body);
			//炮塔放置到车体上
			m_pBody.addChild(m_pGun);
		}
		//
		public function Updata():void{
			PointerMove();
			PointerShot();
		}
		//
		public function Fire():void{
			//发送事件?
			//调用CreateFlyer方法
			//CreateFlyer( this.x+gunX*fun.cos(gun.rotation), this.y+gunX*fun.sin(gun.rotation), initSpeed, this );//坐标\初速\来自
		}
		//
		public function PointerShot():void{
			if(!m_bShotPointer)return;
			var aa:Number = m_iGunAngle-this.rotation;
			var na:Number = aa-m_iGunRotation;
			while(na>180)na-=360;
			while(na<-180)na+=360;
			if(Math.abs(na)<g_pTankGun.maxRad){
				m_iGunAngle=this.rotation+m_iGunRotation;
				if(m_bQuickShot)Fire();//立即开火
				m_bQuickShot=false;
				m_bShotPointer=false;
				return;
			}
			if(na>0)m_iGunAngle+=g_pTankGun.maxRad;
			if(na<0)m_iGunAngle-=g_pTankGun.maxRad;
		}
		//
		public function PointerMove():void{	//指令移动
			var tx:Number=this.x;
			var ty:Number=this.y;
			var na:Number=0;
			
			if(!m_bMovePointer){
				if(m_iRotation!=1000){
					//转到目标朝向
					na=m_iRotation-this.rotation;
					while(na>180)na-=360;
					while(na<-180)na+=360;//调整到-180~180
					if( na>1 )this.rotation+=m_iRad;//调整目标朝向
					if( na<-1)this.rotation-=m_iRad;
				}
				return;//无指令,返回
			}
			//是否需要调整角度 - 应该是,需要左转就左转,需要右转就右转,不需要就算了
			var aa:Number = fun.atg(m_iMy-ty,m_iMx-tx);//貌似此处无意义
			if(m_bFrontWard){//进车
				na = aa-this.rotation;//求角度
				while(na>180)na-=360;
				while(na<-180)na+=360;//调整到-180~180
				if(na>2){//角度没到位
					this.rotation+=m_iRad;
				}
				else if(na<-2){
					this.rotation-=m_iRad;
				}
				else{
					this.rotation=aa;//应该吗?
				}
			}
			else{//倒车
				na = aa-this.rotation-180;//求角度
				while(na>180)na-=360;
				while(na<-180)na+=360;//调整到-180~180
				if(na>2){//角度没到位
					this.rotation+=m_iRad;
				}
				else if(na<-2){
					this.rotation-=m_iRad;
				}
				else{
					var ta:Number=aa-180;
					while(ta<0)ta+=360;
					this.rotation=ta;//应该吗?
				}
			}
			//求平均速度
			var l2:Number=fun.len2(m_iMx-tx,m_iMy-ty);
			//l2/(Math.abs(na)/1);// 转动需要时间为转动角度除以最大转动角速度,距离除以时间等于速度
			var needVx:Number=l2/(Math.abs(na)/m_iRad);
			m_iSpeed+=g_pTankBody.addSpeed;
			if(m_iSpeed>needVx+g_pTankBody.addSpeed)m_iSpeed-=g_pTankBody.addSpeed;
			else if(m_iSpeed>needVx)m_iSpeed=needVx;
			else if(m_iSpeed+g_pTankBody.addSpeed<needVx)m_iSpeed+=g_pTankBody.addSpeed;
			else if(m_iSpeed<needVx)m_iSpeed=needVx;
			if(m_iSpeed>g_pTankBody.maxSpeed)m_iSpeed=g_pTankBody.maxSpeed;//不能超过最大速度
			if(l2>4){
				if(m_bFrontWard){
					this.x+=m_iSpeed*fun.cos(this.rotation);
					this.y+=m_iSpeed*fun.sin(this.rotation);
				}
				else{
					if(m_iSpeed>g_pTankBody.maxSpeed*0.75)m_iSpeed=g_pTankBody.maxSpeed*0.75;
					this.x+=m_iSpeed*fun.cos(this.rotation-180);
					this.y+=m_iSpeed*fun.sin(this.rotation-180);
				}
			}
			else{
				m_bMovePointer=false;
			}
		}
	}
}